package model.dao;

import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;

import com.sun.org.apache.bcel.internal.generic.NEW;

import domain.ClassOneInfo;
import domain.ClassTwoInfo;

public class ClassDao {
	ArrayList<ClassOneInfo> classones = new ArrayList<ClassOneInfo>();
	ArrayList<ClassTwoInfo> classtwos=new ArrayList<ClassTwoInfo>();
	int ClassOneId;
	Connection cn = null;
	Statement st = null;
	PreparedStatement pt = null;
	ResultSet rs = null;

	public void init() {
		try {
			cn = DriverManager.getConnection("proxool.DbPool");
			/*System.out.println("Success loading  MySQL Driver...");*/
		} catch (Exception e) {
			System.out.println("Error loading MySQL Driver...");
			e.printStackTrace();
		}
	}

	public void close() {
		try {
			if (rs != null) {
				rs.close();
			}
			if (pt != null) {
				pt.close();
			}
			if (st != null) {
				st.close();
			}
			if (cn != null) {
				cn.close();
			}

		} catch (Exception e) {
		}
	}

	public void SearchClassOne() {
		init();
		try {
			String url = "select Class_Id,Class_Name from class_info";
			st = cn.createStatement();
			rs = st.executeQuery(url);
			while (rs.next()) {
				ClassOneInfo classone = new ClassOneInfo(rs.getInt(1), rs
						.getString(2));
				classones.add(classone);
			}
		} catch (Exception e) {
		} finally {
			close();
		}
	}
	

	public boolean SearchReClassOne(String name) {
		init();
		try {
			String url = "select Class_Id,Class_Name from class_info where Class_Name='"+name+"'";
			st = cn.createStatement();
			rs = st.executeQuery(url);
			if(rs.next()){
				return true;
			}else
			{
				return false;
			}
		} catch (Exception e) {
			return false;
		} finally {
			close();
		}

	}
	public boolean SearchReClassTwo(String name) {
		init();
		try {
			String url = "select ClassTwo_Id,ClassTwo_Name from classtwo where ClassTwo_Name='"+name+"'";
			st = cn.createStatement();
			rs = st.executeQuery(url);
			if(rs.next()){
				return true;
			}else
			{
				return false;
			}
		} catch (Exception e) {
			return false;
		} finally {
			close();
			
		}
		
	}

	public void insertClassOne(String name) {
		init();
		try {
			String url = "insert into class_info(Class_Name)values('" + name
					+ "'); ";
			st = cn.createStatement();
			st.executeUpdate(url);
		} catch (Exception e) {
		} finally {
			close();
		}
	}
	public void insertClassTwo(String name,int id) {
		init();
		try {
			String url = "insert into classtwo(Class_Id,ClassTwo_Name)values(?,?)";
			pt = cn.prepareStatement(url);
			pt.setInt(1, id);
			pt.setString(2, name);
			pt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
	}

	public ArrayList<ClassOneInfo> getClassones() {
		return classones;
	}
	public void SearchClassTwo(int id)
	{
		init();
		try {
			String url="select ClassTwo_Id,ClassTwo_Name from classtwo where Class_Id="+id;
			st=cn.createStatement();
			rs=st.executeQuery(url);
			while(rs.next())
			{
				ClassTwoInfo two=new ClassTwoInfo(rs.getInt(1), rs.getString(2),id);
				classtwos.add(two);
			}
		} catch (Exception e) {
		}finally
		{
			close();
		}		
	}
	public ArrayList<ClassTwoInfo> getClasstwos() {
		return classtwos;
	}
	public int getClassOneId() {
		return ClassOneId;
	}

}
